package com.freewater.javabase.demo.resourcebundler;

import java.util.*;

public class ResourceBundlerTest {
    public static void main(String[] args) {
        Locale locale = new Locale("zh", "CN");
        final String baseName = "com.freewater.javabase.demo.resourcebundler.data";
        ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale);
        final String key = "user";
        System.out.println(bundle.getString(key));
        //
        locale = new Locale("en", "US");
        bundle = ResourceBundle.getBundle(baseName, locale);
        System.out.println(bundle.getString(key));
        //
        locale = Locale.getDefault();
        bundle = ResourceBundle.getBundle(baseName, locale);
        System.out.println(bundle.getString(key));
        //
        String invalidKey = "xxx";
        try {
            System.out.println(bundle.getString(invalidKey));
        } catch (MissingResourceException e) {
            System.out.println("key：" + invalidKey + "，未找到值");
        }
        //
        ListResourceBundle listResourceBundle = new ListResourceBundle() {
            @Override
            protected Object[][] getContents() {
                final Object[][] objects = new Object[1][1];
                objects[0] = new Object[]{key, new String[]{"zhangsan", "lisi"}};
                return objects;
            }
        };
        final String[] users = listResourceBundle.getStringArray(key);
        System.out.println(Arrays.asList(users));
    }
}
